<?php
namespace server;
use PDO;
class Mysql{
    protected $link = null;
    protected $options = [
        'table' => '',
        'where' => '',
        'field' => '',
        'order' => '',
        'limit' => ''
    ];
    #初始化数据库
    public function __construct(array $config=[])
    {
        $this->connect($config);
    }

    #连接数据库
    public function connect(array $config){
        $dsn = sprintf('mysql:host=%s;dbname=%s;charset=%s',$config['host'],$config['database'],$config['charset']);
        $pdo = new PDO($dsn,$config['user'],$config['password']);
        #设置数据返回的类型
        $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
        #设置所有数据返回的字段值都为小写
        $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
        $this->link = $pdo;
    }

    #查询多条数据数据
    public function query(string $sql,$vars = []){
        #sql预准备
        $sth = $this->link->prepare($sql);
        $sth->execute($vars);
        return $sth->fetchAll();
    }

    #查询一条数据
    public function queryOne(string $sql,$vars = []){
        #sql准备
        $sth = $this->link->prepare($sql);
        $sth->execute($vars);
        return $sth->fetch();
    }

    #执行操作
    public function execute($sql,$vars = []){
        #sql预准备
        $sth = $this->link->prepare($sql);
        return $sth->execute($vars);
    }

    #链式操作之table
    public function table(string $table=null){
        $this->options['table'] = $table;
        return $this;
    }

    #链式操作之field
    public function field(...$field){
        $this->options['field'] = '`'.implode('`,`',$field).'`';
        return $this;
    }

    #链式操作之limit
    public function limit(...$limit){
        $this->options['limit'] = ' LIMIT  '.implode(',',$limit);
        return $this;
    }

    #链式操作之orderBy
    public function orderBy(string $order){
        $this->options['order'] = ' ORDER BY '.$order;
        return $this;
    }

    #链式操作之where
    public function where(string $where){
        $this->options['where'] = ' WHERE '.$where;
        return $this;
    }

    #链式操作之获取结果集
    public function get(){
        $options = $this->options;
        //从这里进行参数的读取
        $sql = "SELECT {$options['field']} FROM {$options['table']} {$options['where']} {$options['order']} {$options['limit']}";
        return $this->query($sql);
    }


    #链式操作之获取一条数据
    public function find(){
        $options = $this->options;
        //进行数据读取
        $sql = "SELECT {$options['field']} FROM {$options['table']} {$options['where']} LIMIT 1";
        return $this->queryOne($sql);
    }

    #链式操作之添加
    public function insert(array $vars = []){
        $value_s = array_values($vars);
        $values = implode(',',array_fill(0,count($vars),'?'));
        $fields = implode(',',array_keys($vars));
        $sql = "INSERT INTO {$this->options['table']} ($fields) VALUES ($values)";
        return $this->execute($sql,$value_s);
    }

    public function get_insert_id(){
        $sql = "select * from lh_user order by id desc limit 1";
        $find = $this->queryOne($sql);
        return $find['id'];
    }

    #链式操作之修改
    public function update(array $vars = []){
        if(empty($this->options['where'])){
            throw new \Exception('不能缺少条件');
        }
        $sql = "UPDATE {$this->options['table']} SET ".implode('=?,',array_keys($vars))."=? {$this->options['where']}";
        return $this->execute($sql,array_values($vars));
    }


    //链式操作之删除
    public function delete(){
        if(empty($this->options['where'])){
            throw new \Exception('缺少删除条件');
        }
        $sql = "DELETE FROM {$this->options['table']} {$this->options['where']}";
        return $this->execute($sql);
    }

}